<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Controller\AbstractController;
use App\Utils\ApiUtils;
use Hyperf\Di\Annotation\Inject;
use App\Validate\CouponValidation;
use App\Service\CouponService;
use App\Constants\ErrorCode;

class CouponController extends AbstractController
{

     /**
     * @Inject
     * @var CouponService
     */
    protected $service;

    /**
     * 明细记录-红包明细-红包日志列表
     * @Author czm
     * @DateTime 2020-04-16
     * @return void
     */
    public function list()
    {
        $pageSize = $this->request->input('pageSize', 15);
        $page = $this->request->input('page', 1);

        //搜索条件
        $start_add_time = $this->request->input('start_add_time', 0);
        $end_add_time = $this->request->input('end_add_time', 0);
        $type = $this->request->input('type', "");
        $unique_num = $this->request->input('unique_num', "");
        $nickname = $this->request->input('nickname', "");
        $where = "";
        if ($start_add_time && $end_add_time) {
            $where = [['add_time', '>', strtotime(strval($start_add_time))], ['add_time', '<', strtotime(strval($end_add_time))]];
        } else if ($start_add_time) {
            $where = [['add_time', '>', strtotime(strval($start_add_time))]];
        } else if ($end_add_time) {
            $where = [['add_time', '<', strtotime(strval($end_add_time))]];
        }
        $result = $this->service->list(compact('pageSize', 'page', 'where', 'nickname', 'unique_num', 'type'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $result['data'], $result['count']));
    }

    public function outputLog(){
        $start_add_time = $this->request->input('start_add_time', 0);
        $end_add_time = $this->request->input('end_add_time', 0);
        $type = $this->request->input('type', "");
        $unique_num = $this->request->input('unique_num', "");
        $nickname = $this->request->input('nickname', "");
        $where = "";
        if ($start_add_time && $end_add_time) {
            $where = [['add_time', '>', strtotime(strval($start_add_time))], ['add_time', '<', strtotime(strval($end_add_time))]];
        } else if ($start_add_time) {
            $where = [['add_time', '>', strtotime(strval($start_add_time))]];
        } else if ($end_add_time) {
            $where = [['add_time', '<', strtotime(strval($end_add_time))]];
        }
        $filename = $this->service->outputLog(compact('where', 'nickname', 'unique_num', 'type'));
        // $filename=$this->service->outputLog($start_time,$end_time);
        return $this->response->download($filename, '话费红包明细.csv');
    }

    /**
     *系统设置-红包券列表
     *czm 2020/4/17
     */
    public function batchList()
    {
        $pageSize = $this->request->input('pageSize', 15);
        $page = $this->request->input('page', 1);
        $batch = $this->request->input('batch', "");

        //搜索条件
        $start_add_time = $this->request->input('start_add_time', 0);
        $end_add_time = $this->request->input('end_add_time', 0);
        $where = "";

        if ($start_add_time && $end_add_time) {
            $where = [['add_time', '>', strtotime(strval($start_add_time))], ['add_time', '<', strtotime(strval($end_add_time))]];
        } else if ($start_add_time) {
            $where = [['add_time', '>', strtotime(strval($start_add_time))]];
        } else if ($end_add_time) {
            $where = [['add_time', '<', strtotime(strval($end_add_time))]];
        }

        if ($this->request->has('pageSize')) {
            $result = $this->service->batchPageList(compact('pageSize', 'page', 'where', 'batch'));
            return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $result['data'], $result['count']));
        } else {
            $result = $this->service->batchList(compact('where', 'batch'));
            return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $result));
        }
    }

    /**
     * 系统设置-红包券-返回批次码 只兼容累计3位数
     * czm 2020/4/17
     */
    public function getBatch()
    {

        $result = $this->service->getBatch();
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $result));
    }

    /**
     * 系统设置-红包券-生成红包券
     * czm 2020/4/17
     */
    public function generateCoupon()
    {
        make(CouponValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $name = $this->request->input('name', "");
        $batch = (int) $this->request->input('batch', 0);
        $type = (int) $this->request->input('type', 1);
        $number = (int) $this->request->input('number', 0);
        $amount = (int) $this->request->input('amount', 0);
        $condition_amount = (int) $this->request->input('condition_amount', 0);
        $start_time = strtotime((string) $this->request->input('start_time', ""));
        $end_time = strtotime((string) $this->request->input('end_time', ""));
        $this->service->generateCoupon(compact('name', 'batch', 'type', 'number', 'amount', 'condition_amount', 'start_time', 'end_time'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '批次生成成功！请稍后查看优惠券生成结果'));
    }

    /**
     * 系统设置-红包券-批次编辑 暂时只允许编辑name
     * czm 2020/4/17
     */
    public function batchEdit()
    {
        make(CouponValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $id = (int) $this->request->input('id', '');
        $name = $this->request->input('name', "");
        $this->service->batchEdit(compact('name', 'id'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '操作成功'));
    }

    /**
     * 系统设置-红包券-查看批次ID下所有识别码
     * czm 2020/4/17
     */
    public function couponBatch()
    {
        make(CouponValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $batch_id = (int) $this->request->input('id', '');
        $pageSize = $this->request->input('pageSize', 1000);
        $page = $this->request->input('page', 1);
        $result = $this->service->couponBatch(compact('pageSize', 'page', 'batch_id'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $result['data'], $result['count']));
    }

    /**
     * 系统设置-红包券-禁用批次 只禁用未使用的
     * czm 2020/4/17
     */
    // public function batchForbid()
    // {
    //     $batch_id = (int) $this->request->input('id', '');
    //     if (empty($batch_id)) {
    //         return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '参数为空'));
    //     }
    //     $info = CouponBatch::find($batch_id);
    //     if (empty($info)) {
    //         return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '批次为空'));
    //     }
    //     $logInfo = $info->toJson(JSON_UNESCAPED_UNICODE);
    //     $info->status = 2;
    //     $info->save();
    //     coupon::OfNotUse()->where('batch_id', $batch_id)->update([
    //         'status' => 4,
    //     ]);
    //     //添加管理员操作日记
    //     $this->adminLogLogic->addLog('禁用红包券批次' . $logInfo);
    //     return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '已禁用'));
    // }

    /**
     * 系统设置-红包券-删除批次 只删除未使用的
     * czm 2020/4/17
     */
    public function batchDel()
    {
        make(CouponValidation::class)->check($this->request->all(), __FUNCTION__)->response();
        $batch_id = (int) $this->request->input('id', '');
        $this->service->batchDel($batch_id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '操作成功'));
    }

    /**
     * 系统设置-红包券-启用
     * czm 2020/4/17
     */
    // public function batchOpen()
    // {
    //     $batch_id = (int) $this->request->input('id', '');
    //     if (empty($batch_id)) {
    //         return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '参数为空'));
    //     }
    //     $info = CouponBatch::withoutGlobalScope('is_del')->find($batch_id);
    //     if (empty($info)) {
    //         return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '批次为空'));
    //     }
    //     $logInfo = $info->toJson(JSON_UNESCAPED_UNICODE);
    //     $info->is_del = 1;
    //     $info->status = 1;
    //     $info->save();

    //     $status = 1;
    //     if (time() < $info->start_time) {
    //         $status = 5;
    //     }
    //     //过期标记
    //     if (time() > $info->end_time) {
    //         $status = 3;
    //     }
    //     Coupon::where('status', 4)->where('batch_id', $batch_id)->update([
    //         'is_del' => 1,
    //         'status' => $status,
    //     ]);
    //     //添加管理员操作日记
    //     $this->adminLogLogic->addLog('启用了红包券批次' . $logInfo);
    //     return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '已启用'));
    // }

    /**
     * 数据统计-红包统计
     * @Author czm
     * @DateTime 2020-04-22
     * @return void
     */
    public function statistics()
    {
        $this->service->addstatistics();
        $pageSize = $this->request->input('pageSize', 15);
        $page = $this->request->input('page', 1);
        $start_time = $this->request->input('start_time', "");
        $end_time = $this->request->input('end_time', "");
        $where = [];
        if ($start_time) {
            //不一定是凌晨生成所以手动修正查询时间戳
            $start_time = strtotime(strval($start_time));
            $start_time = strtotime(date('Y-m-d', $start_time + 3600 * 24));
            $where[] = ['add_time', '>', $start_time];
        }
        if ($end_time) {
            $end_time = strtotime(strval($end_time));
            //不一定是凌晨生成所以手动修正查询时间戳
            $end_time = strtotime(date('Y-m-d', $end_time + 3600 * 24 * 2));
            $where[] = ['add_time', '<', $end_time];
        }

        $result = $this->service->statistics(compact('pageSize', 'page', 'where'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $result['data'], $result['count']));
    }

    /**
     * 数据统计-红包对比
     * @Author czm
     * @DateTime 2020-04-22
     * @return void
     */
    public function compare()
    {
        //搜索条件
        $start_time = $this->request->input('start_time', "");
        $end_time = $this->request->input('end_time', "");

        if ($start_time) {
            $start_time = strtotime(strval($start_time));
        } else {
            return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '对比起始时间不能为空'));
        }

        if ($end_time) {
            $end_time = strtotime(strval($end_time));
        } else {
            return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '对比截至时间不能为空'));
        }

        if ($end_time < $start_time) {
            return $this->response->json(ApiUtils::send(ErrorCode::ERR_BUESSUS, '对比截至时间不能小于起始时间'));
        }
        $result = $this->service->compare(compact('start_time', 'end_time'));
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS, '获取成功', $result));
    }
}
